<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Config;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $lbList = db('cms_archives')->where(['channel_id'=>'24'])->page(1, 3)->select();

        $yslist = db('cms_archives')->where(['channel_id'=>'1'])->page(1, 10)->select();
        foreach ($yslist as $k=>$v){
            $yslist[$k]['createtime'] = date('Y-m-d',$v['publishtime']);

        }
        $kylist = db('cms_archives')->where(['channel_id'=>'25'])->page(1, 5)->order('publishtime desc')->select();
        foreach ($kylist as $k=>$v){
            $kylist[$k]['createtime'] = date('Y-m-d',$v['publishtime']);
        }
        $data['count'] = db('identify')->count();
        $data['lbList'] = $lbList;
        $data['ysList'] = $yslist;
        //$data['kyList'] = $kylist;

        $this->success('请求成功',$data);
    }

    /**
     * 文章详情
     *
     */
    public function detail()
    {
        $id = (int)$this->request->request('id', 0);
        if(!$id){
            $this->error('ID不能为空');
        }
        $data = db('cms_addonnews')->where(['id'=>$id])->find();
        if(!$data){
            $this->error('文章不存在');
        }
        //$data['content'] = $this->ClearHtml($data['content']);
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
        $content = preg_replace($pregRule, '<img src="'.$this->domain().'${1}" data-filename="filename" style="width:100%">', $data['content']);
        $data['content']=$content;


        $this->success('请求成功', $data);
    }

    /**
     * 单页详情
     *
     */
    public function pageDetail()
    {
        $id = (int)$this->request->request('id', 0);
        if(!$id){
            $this->error('ID不能为空');
        }
        $data = db('cms_page')->where(['id'=>$id])->find();
        if(!$data){
            $this->error('文章不存在');
        }
        //$data['content'] = $this->ClearHtml($data['content']);
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
        $content = preg_replace($pregRule, '<img src="'.$this->domain().'${1}" data-filename="filename" style="width:100%">', $data['content']);
        $data['content']=$content;

        $this->success('请求成功', $data);
    }


    function domain()
    {
        //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        return $domain;
    }

    function removeImgAttr($content)
    {

        $content = preg_replace(
            array(

                '/(<img [^<>]*?)width=.+?[\'|\"]/',
                '/(<img.*?)((height)=[\'"]+[0-9|%]+[\'"]+)/',
                '/(<img.*?)((style)=[\'"]+(.*?)+[\'"]+)/',
            ) , array('$1 style="width:90%" ', '$1', '$1') , $content);
        return $content;
    }

    Function ClearHtml($content) {

        $content = preg_replace("/<a[^>]*>/i", "", $content);

        $content = preg_replace("/<\/a>/i", "", $content);

        $content = preg_replace("/<div[^>]*>/i", "", $content);

        $content = preg_replace("/<\/div>/i", "", $content);

        $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容

        $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式

        $content = preg_replace("/face=.+?['|\"]/",'',$content);//去除样式只允许小写正则匹配没有带 i 参数

        return $content;

    }
}
